package com.boarsoft.boar.label.biz;

import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.boarsoft.boar.entity.ObjLabel;
import com.boarsoft.boar.label.ObjLabelBiz;
import com.boarsoft.common.Util;
import com.boarsoft.common.dao.PagedResult;
import com.boarsoft.hibernate.biz.SimpleBizImpl;

/**
 * 
 * @author Mac_J
 *
 */
@Component
public class ObjLabelBizImpl extends SimpleBizImpl implements ObjLabelBiz {

	@Override
	@Transactional
	public void save(ObjLabel o) {
		if (Util.strIsEmpty(o.getId())) {
			dao.save(o);
		} else {
			dao.merge(o);
		}
	}

	@Override
	@Transactional(readOnly = true)
	public PagedResult<ObjLabel> list(String objId, Integer objType, String key, String orderBy, int pageNo, int pageSize) {
		StringBuilder sb = new StringBuilder();
		sb.append("from ").append(ObjLabel.class.getName()).append(" where 1=1");
		if (Util.strIsNotEmpty(objId)) {
			sb.append(" and objId='").append(objId).append("'");
		}
		if (objType != null) {
			sb.append(" and objType=").append(objType);
		}
		if (Util.strIsNotEmpty(key)) {
			sb.append(" and (key like '%").append(key);
			sb.append("%' or value like '%").append(key);
			sb.append("%') ");
		}
		int total = dao.getTotal("select count(1) ".concat(sb.toString()));
		if (Util.strIsNotEmpty(orderBy)) {
			sb.append(" order by ").append(orderBy);
		} else {
			sb.append(" order by objId");
		}
		List<ObjLabel> lt = dao.list(sb.toString(), ObjLabel.class, pageNo, pageSize);
		return new PagedResult<ObjLabel>(total, lt, pageNo, pageSize);
	}

	@Override
	@Transactional(readOnly = true)
	public List<ObjLabel> list(String objId, Integer objType, String key, String value) {
		StringBuilder sb = new StringBuilder();
		sb.append("from ").append(ObjLabel.class.getName()).append(" where 1=1");
		if (Util.strIsNotEmpty(objId)) {
			sb.append(" and objId='").append(objId).append("'");
		}
		if (objType != null) {
			sb.append(" and objType=").append(objType);
		}
		if (Util.strIsNotEmpty(key)) {
			sb.append(" and key='").append(key).append("'");
		}
		if (Util.strIsNotEmpty(value)) {
			sb.append(" and value='").append(value).append("'");
		}
		return dao.list(sb.toString(), ObjLabel.class);
	}

	@Override
	@Transactional(readOnly = true)
	public ObjLabel find(Integer objType, String key, String value) {
		StringBuilder sb = new StringBuilder();
		sb.append("from ").append(ObjLabel.class.getName())//
				.append(" where objType=").append(objType)//
				.append(" and key='").append(key)//
				.append("' and value='").append(value).append("'");//
		return dao.find(sb.toString(), ObjLabel.class);
	}

	@Override
	@Transactional(readOnly = true)
	public ObjLabel find(String objId, String key, Integer objType) {
		StringBuilder sb = new StringBuilder();
		sb.append("from ").append(ObjLabel.class.getName())//
				.append(" where objId='").append(objId)//
				.append("' and key='").append(key).append("'");
		if (objType != null) {//
			sb.append(" and objType=").append(objType);//
		}
		return dao.find(sb.toString(), ObjLabel.class);
	}

	@Override
	@Transactional(readOnly = true)
	public List<ObjLabel> list(Integer objType, String key, String value) {
		StringBuilder sb = new StringBuilder();
		sb.append("from ").append(ObjLabel.class.getName()).append(" where 1=1");
		if (objType != null) {
			sb.append(" and objType=").append(objType);
		}
		if (Util.strIsNotEmpty(key)) {
			sb.append(" and key='").append(key).append("'");
		}
		if (Util.strIsNotEmpty(value)) {
			sb.append(" and value='").append(value).append("'");
		}
		return dao.list(sb.toString(), ObjLabel.class);
	}

	@Override
	@Transactional
	public void delete(String ids) {
		for (String id : ids.split(",")) {
			dao.delete(ObjLabel.class, id);
		}
	}

	@Override
	@Transactional
	public void delete(String objId, String objType) {
		StringBuilder sb = new StringBuilder();
		sb.append("delete from ").append(ObjLabel.class.getName())//
				.append(" where objId=? and objType=?");
		Query q = dao.createQuery(ObjLabel.class, sb.toString());
		q.setParameter(1, objId);
		q.setParameter(2, objType);
		q.executeUpdate();
	}

	@Override
	@Transactional(readOnly = true)
	public ObjLabel get(String id) {
		return dao.get(ObjLabel.class, id);
	}
}